﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.common;
using rock.DAL;

namespace rock.UI.system
{
    public partial class FormChangePwd : Form
    {
        public FormChangePwd()
        {
            InitializeComponent();

            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

       private  Boolean check(){
            try 
	        {	        
		         StringUtils.checkText(this.textBox2Old.Text, 2, 30, "原密码");
                 StringUtils.checkText(this.textBox3New.Text, 2, 30, "新密码");
                 StringUtils.checkText(this.textBox4New2.Text, 2, 30, "确认密码");
                 return true;
	        }
	        catch (Exception e)
	        {
                MessageBox.Show(e.Message, "提示");
                return false; 
	        }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (check())
            {
                String account = Manager.getInstance().userAccount.ACCOUNT;
                String password = Manager.getInstance().userAccount.PASSWORD;
                if (!this.textBox2Old.Text.Trim().Equals(password.Trim()))
                {
                    MessageBox.Show("请输入正确的原密码!", "提示");
                    return;
                }
                if (!this.textBox3New.Text.Trim().Equals(this.textBox4New2.Text.Trim()))
                {
                    MessageBox.Show("两次新密码输入不一致!", "提示");
                    return;
                }

                DalUSER_ACCOUNT aDalUSER_ACCOUNT = new DalUSER_ACCOUNT();

                Boolean ok = aDalUSER_ACCOUNT.UpdatePwd(account, password, this.textBox4New2.Text.Trim());
                if (ok)
                {
                    Manager.getInstance().userAccount.PASSWORD = this.textBox4New2.Text.Trim();
                    this.Close();
                    MessageBox.Show("修改成功.下次登陆请使用新密码.", "提示");
                    
                }
                else
                {
                    MessageBox.Show("修改失败", "提示");
                }
            }
        }

        private void FormChangePwd_Load(object sender, EventArgs e)
        {
            String account = Manager.getInstance().userAccount.ACCOUNT;
            this.textBox1Account.Text = account; 
        }
    }
}
